/**
 * Copyright (c) 2020 INESC TEC <https://www.inesctec.pt>
 *
 * This Source Code Form is subject to the terms of the European Union
 * Public License, v. 1.2. If a copy of the EUPL was not distributed with
 * this file, You can obtain one at https://opensource.org/licenses/EUPL-1.2.
 *
 * SPDX-License-Identifier: EUPL-1.2
 */

/**
 * Copyright (c) 2020 Ubique Innovation AG <https://www.ubique.ch>
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 *
 * SPDX-License-Identifier: MPL-2.0
 */

import DP3TSDK

/// Only CodedError should be used in the UI and UI State
/// All errors that are generated by the SDK or the app should conform to this protocol
protocol CodedError: Error {
    var errorTitle: String? { get }
    var errorCodeString: String? { get }
}

extension NetworkError: LocalizedError, CodedError {
    var errorTitle: String? {
        nil
    }

    var errorDescription: String? {
        switch self {
        case .networkError:
            return "network_error"
        case .statusError(code: _): fallthrough
        case .parseError: fallthrough
        case .unexpected(error: _):
            return "unexpected_error_title"
        }
    }

    var errorCodeString: String? {
        switch self {
        case .networkError:
            return "ICNETE"
        case let .statusError(code: code):
            return "IBST\(code)"
        case .parseError:
            return "ICPARS"
        case let .unexpected(error: error):
            let nsError = error as NSError
            return "IUNXN\(nsError.code)"
        }
    }
}

extension CertificateValidationError: LocalizedError, CodedError {
    var errorTitle: String? {
        nil
    }

    var errorDescription: String? {
        return "certificate_validation_error"
    }

    var errorCodeString: String? {
        return "ICERT"
    }
}
